<template>

  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="dialogVisible" @close="btclose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="refmanager" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="chekmanager">
          <el-option
            v-for="item in employee"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="commit">确定</el-button>
        <el-button size="small" @click="btclose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>

</template>

<script>
import { department, addDeldepartment, ByIdgetdepartment, Editdepartment } from '@/api/department'
import { simple } from '@/api/employee'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      redirect: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const chekName = async(rule, value, callback) => {
      let isDefault = false
      const { depts } = await department()
      console.log(this.nodeData)
      isDefault = depts.filter(item => item.pid === this.nodeData.id)
        .some(value => value.name === this.formData.name)
      if (!isDefault) {
        callback()
      } else {
        callback(new Error('同一部门，子部门名称不能相同'))
      }
    }
    const chekCode = async(rule, value, callback) => {
      let isDefault = false
      if (this.formData.id) {
        isDefault = this.depts.some(item => item.code === value && item.id !== this.nodeData.id)
      } else {
        isDefault = this.depts.some(item => value === item.code)
      }
      if (!isDefault) {
        callback()
      } else {
        callback(new Error('部门编号是唯一的'))
      }
    }
    return {
      depts: [],
      employee: [],
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: chekName }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: chekCode }],
        manager: [{ required: true, message: '部门负责人不能为空' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  async mounted() {
    this.depts = (await department()).depts
    this.employee = await simple()
  },
  methods: {
    chekmanager() {
      this.$refs.refmanager.validateField('manager')
    },
    async commit() {
      if (this.formData.id) {
        await Editdepartment(this.formData, this.formData.id)
      } else {
        await this.$refs.refmanager.validate()
        await addDeldepartment({ ...this.formData, pid: this.nodeData.id })
      }
      // 修饰符修改父组件的值固定写法,
      // this.$emit('update:要修改的属性名'，值)
      this.$emit('update:dialogVisible', false)
      this.$emit('updata')
    },
    btclose() {
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
      this.$emit('update:dialogVisible', false)
      // 只能清除绑定的字段与错误
      this.$refs.refmanager.resetFields()
    },
    async ByIdgetdepartment(id) {
      this.formData = await ByIdgetdepartment(id)
    }
  }
}
</script>

<style>
</style>
